package cn.edu.cqu.bysj.wm.gui;

import java.awt.Component;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;

public class ImageButton extends JButton {
	/**
	 * create the image button
	 */
	public ImageButton() {
		initUI();
	}

	public ImageButton(ImageIcon icon) {
		setSize(icon.getImage().getWidth(null), icon.getImage().getHeight(null));
		setIcon(icon);
		initUI();
	}

	public void initUI() {
		setMargin(new Insets(0, 0, 0, 0));// 将边框外的上下左右空间设置为0
		setIconTextGap(0);// 将标签中显示的文本和图标之间的间隔量设置为0
		// setBorderPainted(false);//不打印边框
		// setBorder(null);//除去边框
		setText(null);// 除去按钮的默认名称
		setFocusPainted(false);// 除去焦点的框
		setContentAreaFilled(false);// 除去默认的背景填充
		// 不对Button进行填充，就是按键是透明的
		setContentAreaFilled(false);
		// 按键不能获得焦点
		setFocusable(false);
		// 设置按键的边框为雕刻样式
		setBorder(BorderFactory.createEtchedBorder());
		// 系统不自动绘制按键边界（这个边界在鼠标放上去之后才绘制）
		setBorderPainted(false);
		this.addMouseListener(mouseListener);
	}

	private final MouseListener mouseListener = new MouseAdapter() {
		@Override
		public void mouseEntered(MouseEvent event) {
			// 鼠标移入按键，绘制按键边界
			Component c = event.getComponent();
			if (c instanceof AbstractButton)
				((AbstractButton) c).setBorderPainted(true);
		}

		@Override
		public void mouseExited(MouseEvent event) {
			// 鼠标移出按键，不绘制按键边界
			Component c = event.getComponent();
			if (c instanceof AbstractButton)
				((AbstractButton) c).setBorderPainted(false);
		}

	};

	public static JButton makeNavigationButton(String imageName,
			String actionCommand, String toolTipText, String altText,
			ActionListener actionListener) {
		return makeNavigationButton(imageName, actionCommand, toolTipText,
				altText, actionListener, 40, 40);
	}

	public static JButton makeNavigationButton(String imageName,
			String actionCommand, String toolTipText, String altText,
			ActionListener actionListener, int width, int height) {

		// look for the image
		String imgLocation = "/" + imageName;
		URL imageURL = ImageButton.class.getResource(imgLocation);
		String imageUrl = "resources/icons/" + imageName;

		// create and initialize the button.
		JButton button = new ImageButton();

		button.setActionCommand(actionCommand);
		button.setToolTipText(toolTipText);
		button.addActionListener(actionListener);
		ImageIcon icon = null;
		if (imageURL != null) {
			if (imageURL.getPath().toLowerCase().endsWith("ico")) {

			} else if (imageURL.getPath().toLowerCase().endsWith("png")) {
				try {
					Image image = ImageIO.read(imageURL).getScaledInstance(
							width, height, Image.SCALE_SMOOTH);
					icon = new ImageIcon(image);
				} catch (IOException e) {
					e.printStackTrace();
				}
			} else {
				icon = new ImageIcon(imageUrl);
			}
			if (icon != null)
				button.setIcon(icon);
		} else { // no image found
			button.setText(altText);
			System.err.println("Resource not found: " + imgLocation);
		}
		return button;
	}

}